<?php
class ControllerBase extends CController
{
    /**
    * Message to be displayed to user
    *
    * @var string
    */
    protected $message;

    /**
    * Array of view data
    *
    * @var array
    */
    public $data = array();
    
    public $serviceResult;

    public function __construct($id, $module = null){
        parent::__construct($id, $module);
        if (Yii::app()->request->isPostRequest && !Yii::app()->request->isAjaxRequest && isset($_POST['post_service_function']))
        {
            $this->serviceResult = WCore::service($_POST['post_service_function'],$_POST);    
        }
    }

    public function post($name, $default = null, $filter = ''){
        $input = new XInput($_POST);
        return $input->getInput($name, $default, $filter);
    }

    public function get($name, $default = null, $filter = ''){
        $input = new XInput($_GET);
        return $input->getInput($name, $default, $filter);
    }

    public function cookie($name, $default = null, $filter = ''){
        $input = new XInput($_COOKIE);
        return $input->getInput($name, $default, $filter);
    }

    /**
    * Get message of current page and also previous page
    *
    * Message from previous page will be prepended
    */
    public function getMessage(){
        $message = Yii::app()->Session['Message'];
        $message .= "<br/>".$this->message;
        unset(Yii::app()->Session['Message']);
        return $message;
    }
    /**
    * Set message for the current page
    *
    * @param mixed $value
    */
    public function setMessage($value){ $this->message = $value;}

    /**
    * Redirect page but save errors and message to session if needed
    *
    * @param mixed $url
    * @param mixed $terminate
    */
    public function redirect($url, $terminate = true, $statusCode=302){
        $errorHtml = $this->getUserErrors();
        if ($errorHtml != '')
            Yii::app()->Session['ErrorMessage'] = $errorHtml;
        if ($this->message != '')
            Yii::app()->Session['Message'] = $this->message;
        parent::redirect($url, $terminate);
    }

    /**
    * Get user error messages and translate into HTML content
    * @return string HTML error content
    */
    public function getUserErrors(){
        $errors = Yii::app()->getErrorHandler()->getUserExceptions();
        $c = count($errors);
        if ($c == 0) return '';


        $html = "<ul>\n";
        for ($i = 0; $i < $c; $i++){
            $e = $errors[$i];
            $html .= "<li>".$e->getErrorMessage()."</li>\n";
        }
        $html .= "</ul>\n";
        return $html;
    }

    /**
    * Get error for displaying to user
    *
    * Error message in session (from previous page) will be prepended
    */
    public function getErrors(){
        //Get error message from previous page (which may redirect user to this page)
        $html = Yii::app()->Session['ErrorMessage'];
        //Append error message in this page after processing
        $html .= "<br/>".$this->getUserErrors();
        //Error is surely displayed to user, we can clean session
        unset(Yii::app()->Session['ErrorMessage']);
        return $html;
    }


	/**
    * define form after postback
    */
    public function defineFormAfterPostBack($formId,$submitName){
    	$request = Yii::app()->request;
        if ($request->isPostRequest && !$request->isAjaxRequest && $request->getPost('FORM_ID','') == $formId && isset($_POST[$submitName])) {
			return true;
        }
    }
}
?>
